class EventBus {
  constructor() {
    this.events = {};
  }

  emit(eventName, data) {
    if (this.events[eventName]) {
      this.events[eventName].forEach((fn) => fn(data));
    }
  }

  on(eventName, fn) {
    this.events[eventName] = this.events[eventName] || [];
    this.events[eventName].push(fn);
  }

  off(eventName, fn) {
    if (this.events[eventName]) {
      this.events[eventName] = this.events[eventName].filter(
        (eventFn) => eventFn !== fn
      );
    }
  }
}

export default {
  install: (app, options) => {
    const $bus = new EventBus();
    app.config.globalProperties.$bus = $bus;
  },
};
